<?php
/**
 * Date: 6/16/12
 * Time: 2:27 PM
 */

require_once '../bootstrap.php';
include_once '../includes/formvalidator.php';
include_once '../includes/utils.php';

// Must login
if (empty($_SESSION['admin_id'])) {
	header('Location: login.php');
}

$__page = "ads";

$sport_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$selected_sport = $my_db->select_row('SELECT * FROM sports WHERE id = ?', array($sport_id));

if (isset($_GET['id']) && empty($selected_sport)) {
	echo 'There are no such sport in our system!';
} else {
	$data = $selected_sport;
	if (isset($_POST['Submit'])){
		$data = $_POST;
		$form_err = array();
		if (empty($data['name'])){
			$form_err [] = 'Sports name required!';
		}else{
			$files = $_FILES;
			$imgOn = !empty($files['imgOn']['name'])?$files['imgOn']:NULL;
			$imgOff = !empty($files['imgOff']['name'])?$files['imgOff']:NULL;
			$upload_folder = "uploads/";
			if ($imgOn !== NULL){
				if ($imgOn['type'] != "image/jpeg" && $imgOn['type']!= "image/png"){
					$form_err [] = 'Image on must be jpg or png file!';
				}else{
					$info = pathinfo($imgOn['name']);
					$imgOnName = str_replace(' ','_',$data['name']) ."_ON.".$info['extension'];
					$imgOnPath = $upload_folder . $imgOnName;
					move_uploaded_file($imgOn['tmp_name'], $imgOnPath);
				}
			}
			if ($imgOff !== NULL){
				if ($imgOff['type'] != "image/jpeg" && $imgOff['type'] != "image/png"){
					$form_err [] = 'Image off must be jpg or png file!';
				}else{
					$info = pathinfo($imgOff['name']);
					$imgOffName = str_replace(' ','_',$data['name']) ."_Off.".$info['extension'];
					$imgOffPath = $upload_folder . $imgOffName;
					move_uploaded_file($imgOff['tmp_name'], $imgOffPath);
				}
			}
			if (empty($selected_sport) && ($imgOff===NULL || $imgOn === NULL)) {
				$form_err [] = 'Files must be upload!';
			}
		}
		// Validation
		if (empty($form_err)) {
			// Very important!!
			if (!empty($selected_sport)) {
				// ACTION = EDIT
				$data_table = array($data['name']);
				$sql = 'UPDATE sports SET `name` = ?';
				if (isset($imgOnPath)){
					$sql .= ', `image_on` = ?';
					$data_table[] = $imgOnPath;
				}
				if (isset ($imgOffPath)){
					$sql .= ', `image_off` = ?';
					$data_table[] = $imgOffPath;
				}
				$sql .= ' WHERE id = ?';
				$data_table[] = $_GET['id'];
				$return_code = $my_db->execute($sql, $data_table);
			} else {
				// ACTION = ADD
				$data_table = array ($data['name'], $imgOnPath, $imgOffPath);
				$return_code = $my_db->execute('INSERT sports (`name`,`image_on`,`image_off`) VALUES (?, ?, ?)', $data_table);
			}

			if ($return_code > 0) {
				$mess = getADMSuccess("Sport have been updated.");
				$data = array();
			}

		} else {
			foreach ($form_err as $error) {
				$errors .= li($error);
			}
			$mess = getADMError($errors);
		}
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="X-UA-Compatible" content="IE=7"/>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"/>
	<title><?php echo $title; ?></title>

	<style type="text/css" media="all">
		@import url("css/style.css");
		@import url("css/jquery.wysiwyg.css");
		@import url("css/facebox.css");
		@import url("css/visualize.css");
		@import url("css/date_input.css");
	</style>

	<!--[if lt IE 8]>
	<style type="text/css" media="all">@import url("css/ie.css");</style><![endif]-->
	<link rel="stylesheet" href="css/jquery-ui.css" type="text/css" media="screen" />
	<script language="javascript" src="js/jquery.min.js"></script>
	<script language="javascript" src="js/jquery.ui.core.min.js"></script>
	<script language="javascript" src="js/jquery.ui.datepicker.min.js"></script>
	<SCRIPT language=Javascript>
		function isNumberKey(evt)
		{
			var charCode = (evt.which) ? evt.which : event.keyCode
			if (charCode > 31 && (charCode < 48 || charCode > 57))
				return false;
			return true;
		}
		function validate() {
			var ok =true;
			var errtext = '';
			$(".require").each(function () {
				if ($(this).val() == "") {
					$(this).addClass("error");
					ok = false;
				} else {
					$(this).removeClass("error");
				}
			});
			if (!ok){
				errtext += "<li>You must fill full data</li>";
			}
			return ok;
		}
	</SCRIPT>

</head>


<body>

<div id="hld">

	<div class="wrapper">        <!-- wrapper begins -->

		<?php include("__menu.php");?>
		<!-- #header ends -->
		<!-- .block ends -->
		<div class="block">
			<div class="block_head">
				<div class="bheadl"></div>
				<div class="bheadr"></div>
				<h2>Add Edit Sport</h2>
			</div>
			<!-- .block_head ends -->
			<div class="block_content">
				<?php echo $mess;?>
				<p>
				<form enctype="multipart/form-data" action="" name="f1" id="f1" method="post" class="theForm" onsubmit="return validate();">
					<table width="100%" border="0" cellspacing="2" cellpadding="2">
						<tr>
							<td width="38%" class="theField">Name :</td>
							<td width="62%"><input name="name" value="<?php echo _getEcho($data,'name'); ?>" type="text" class="text small require" id="url" size="30" maxlength="255" /></td>
						</tr>
						<tr>
							<td class="theField"> Image On :</td>
							<td>
								<input name="imgOn" type="file" class="text medium" id="imgOn" size="30" accept="image/*" /></td>
						</tr>
						<tr>
							<td class="theField">Image Off :</td>
							<td><input name="imgOff" type="file" class="text medium" id="imgOff" size="30" accept="image/*" /></td>
						</tr>
						<tr>
							<td></td>
							<td colspan="2">
								<input name="Submit" type="submit" class="submit mid" value=" Update "/>
								<input name="Reset" type="reset" class="submit mid" value=" Reset "/>
							</td>
						</tr>
					</table>

				</form>
				</p>
			</div>
			<!-- .block_content ends -->
		</div>
		<!-- .block ends -->
		<!-- .block ends -->
		<!-- .block.small.left ends -->
		<!-- .block.small.right ends -->
		<!-- .block ends -->
		<!-- .block ends -->
		<!-- .leftcol ends -->
		<?php include("__footer.php"); ?>
	</div>
	<!-- wrapper ends -->

</div>
<!-- #hld ends -->
</body>
</html>